package au.com.handsunited.web.controller.project;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.model.Project;
import au.com.handsunited.service.ProjectService;
import au.com.handsunited.service.ServiceCategoryService;
import au.com.handsunited.utils.constants.RegionConstants;
import au.com.handsunited.web.command.project.ProjectSearchCommand;

/**
 * @author arunda
 *
 */
public class ProjectSearchController extends SimpleFormController {


	private ProjectService projectService;
	private ServiceCategoryService serviceCategoryService;
	private RegionConstants regionConstants;

	private static Logger logger = LoggerFactory.getLogger(ProjectSearchController.class);

	/**
	 *
	 */
	public ProjectSearchController() {
		super();
		setCommandClass(ProjectSearchCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		ProjectSearchCommand projectSearchCommand = (ProjectSearchCommand) command;

		List<Project> projects = projectService.searchForProjectsByCriteria(
				projectSearchCommand.getProject());

		ModelAndView mav = new ModelAndView(
				getSuccessView(), getCommandName(), projectSearchCommand);

		mav.addObject("projects", projects);

		Map referenceData = referenceData(request, errors.getTarget(), errors);
		if (isFormSubmission(request) && (referenceData != null)) {
			mav.addAllObjects(referenceData);
		}

		return mav;
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("states", regionConstants.getStates());
//        data.put("serviceCategories", serviceCategoryService.getAllServiceCategories());

		return data;
	}


	/**
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {

		ProjectSearchCommand command = new ProjectSearchCommand();
		command.setServiceCategoryMap(serviceCategoryService.getServiceCategoryMap());
		return command;
	}


	/**
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#isFormSubmission(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected boolean isFormSubmission(HttpServletRequest request) {
		return super.isFormSubmission(request);
	}




	/**
	 * @param projectService the projectService to set
	 */
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}


	/**
	 * @param serviceCategoryService the serviceCategoryService to set
	 */
	public void setServiceCategoryService(
			ServiceCategoryService serviceCategoryService) {
		this.serviceCategoryService = serviceCategoryService;
	}


	/**
	 * @param regionConstants the regionConstants to set
	 */
	public void setRegionConstants(RegionConstants regionConstants) {
		this.regionConstants = regionConstants;
	}


}
